package my.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import my.bean.Cart;
import my.dao.ICartDao;
import my.dao.lmpl.ICartDaoImpl;
import my.dbUtil.DBUtils;
import my.service.ICartService;

public class ICartServiceImpl implements ICartService {

	@Override
	public int addCart(Cart cart) throws SQLException {
		Connection conn = null;
		int record = 0;
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			ICartDao cartDao = new ICartDaoImpl(conn);
			record = cartDao.addCart(cart);
			return record;
		} catch (SQLException e) {
			System.err.println("新增失败!尝试进行事务回滚!");
			try {
				conn.rollback();
				System.err.println("事务回滚成功!");
			} catch (SQLException e2) {
				System.err.println("事务回滚失败!");
				e2.printStackTrace();
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e2) {
				e2.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public int deleteCart(int id) throws SQLException {
		Connection conn = null;
		int record = 0;
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			ICartDao cartDao = new ICartDaoImpl(conn);
			record = cartDao.deleteCart(id);
			return record;
		} catch (SQLException e) {
			System.err.println("删除失败!尝试进行事务回滚!");
			try {
				conn.rollback();
				System.err.println("事务回滚成功!");
			} catch (SQLException e2) {
				System.err.println("事务回滚失败!");
				e2.printStackTrace();
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e2) {
				e2.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public int modifyCart(Cart cart) throws SQLException {
		Connection conn = null;
		int record = 0;
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			ICartDao cartDao = new ICartDaoImpl(conn);
			record = cartDao.modifyCart(cart);
			return record;
		} catch (SQLException e) {
			System.err.println("修改失败!尝试进行事务回滚!");
			try {
				conn.rollback();
				System.err.println("事务回滚成功!");
			} catch (SQLException e2) {
				System.err.println("事务回滚失败!");
				e2.printStackTrace();
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e2) {
				e2.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public List<Cart> findByTeaName(int id, String name) {
		List<Cart> list = null;
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			ICartDao cartDao = new ICartDaoImpl(conn);
			list = cartDao.findByTeaName(id, name);
			return list;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public List<Cart> findByTeaId(int uid, int tid) {
		List<Cart> list = null;
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			ICartDao cartDao = new ICartDaoImpl(conn);
			list = cartDao.findByTeaId(uid, tid);
			return list;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public List<Cart> findByUserid(int id) {
		List<Cart> list = null;
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			ICartDao cartDao = new ICartDaoImpl(conn);
			list = cartDao.findByUserid(id);
			return list;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public Cart findByCartId(int id) {
		Cart cart = null;
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			ICartDao cartDao = new ICartDaoImpl(conn);
			cart = cartDao.findByCartId(id);
			return cart;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

}
